package com.action.admin.website;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.action.BaseAction;
import com.dao.GoodsTypeDao;
import com.entity.Employee;
import com.entity.Function;
import com.entity.GoodsType;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.ResultPath;

/**
 * 
 * @author Administrator
 * 
 */
// @ParentPackage("json-default")
@ParentPackage("extendsjson-default")
@Namespace("/website")
@ResultPath("/WEB-INF/pages")
public class WebAction extends BaseAction {

	private static final long serialVersionUID = 1L;
	private static Logger logger = Logger.getLogger(WebAction.class);

	@Action(value = "top", results = { @Result(name = "success", location = "/top.jsp") })
	public String top() {
		try {
			list = goodsTypeDao.findTopNod();
			List list2 = goodsTypeDao.listGoodsType();
			map = listToMap(list, list2);
			list3 = goodsTypeDao.listGoodsType();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}

	@Action(value = "left", results = { @Result(name = "success", location = "/leftmenu.jsp") })
	public String left() {
		try {
			list = goodsTypeDao.findTopNod();// 一级菜单
			map = new HashMap();
			map2 = new HashMap();
			// 二级菜单
			for (GoodsType gtype : list) {
				list2 = goodsTypeDao.findParentGoodsTypeById(gtype.getId());
				System.out.println("gtype.getId()== " + gtype.getId());
				map.put(gtype.getId(), list2);
				// 三级菜单
				for (GoodsType g2type : list2) {
					list3 = goodsTypeDao.findParentGoodsTypeById(g2type.getId());
					System.out.println("g2type.getId()== " + g2type.getId());
					map2.put(g2type.getId(), list3);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}
	
	@Action(value = "index", results = { @Result(name = "success", location = "/site_index.jsp") })
	public String index() {
		try {
			list = goodsTypeDao.findTopNod();// 一级菜单
			map = new HashMap();
			map2 = new HashMap();
			// 二级菜单
			for (GoodsType gtype : list) {
				list2 = goodsTypeDao.findParentGoodsTypeById(gtype.getId());
				System.out.println("gtype.getId()== " + gtype.getId());
				map.put(gtype.getId(), list2);
				// 三级菜单
				for (GoodsType g2type : list2) {
					list3 = goodsTypeDao.findParentGoodsTypeById(g2type.getId());
					System.out.println("g2type.getId()== " + g2type.getId());
					map2.put(g2type.getId(), list3);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}

	/**
	 * 以父子关系将功能分类
	 * 
	 * @param list
	 * @return
	 */
	public Map listToMap(List<GoodsType> list, List<GoodsType> list2) {
		Map<Integer, List<GoodsType>> map = new HashMap<Integer, List<GoodsType>>();
		int parentid = 0;
		for (GoodsType goodsType2 : list) {
			parentid = goodsType2.getId();
			if (map.containsKey(parentid)) {
				List<GoodsType> l = map.get(parentid);
				for (GoodsType goodsType : list2) {
					if (goodsType.getParentid() == parentid)
						l.add(goodsType);
				}
			} else {
				List<GoodsType> l = new ArrayList<GoodsType>();
				for (GoodsType goodsType : list2) {
					if (goodsType.getParentid() == parentid)
						l.add(goodsType);
				}
				map.put(parentid, l);
			}
		}
		return map;
	}

	private GoodsTypeDao goodsTypeDao = (GoodsTypeDao) ioc.newInstance(GoodsTypeDao.class);
	private GoodsType goodsType;

	private List<GoodsType> list, list2, list3;
	private String id;
	boolean flag = false;
	private Map map, map2;

	public List<GoodsType> getList() {
		return list;
	}

	public void setList(List<GoodsType> list) {
		this.list = list;
	}

	public List<GoodsType> getList2() {
		return list2;
	}

	public void setList2(List<GoodsType> list2) {
		this.list2 = list2;
	}

	public List<GoodsType> getList3() {
		return list3;
	}

	public void setList3(List<GoodsType> list3) {
		this.list3 = list3;
	}

	public Map getMap2() {
		return map2;
	}

	public void setMap2(Map map2) {
		this.map2 = map2;
	}

	public Map getMap() {
		return map;
	}

	public void setMap(Map map) {
		this.map = map;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public GoodsType getGoodsType() {
		return goodsType;
	}

	public void setGoodsType(GoodsType goodsType) {
		this.goodsType = goodsType;
	}

	public boolean isFlag() {
		return flag;
	}

	public void setFlag(boolean flag) {
		this.flag = flag;
	}

}
